package reco.db.validation

import reco.db.field.*
import reco.db.*

scenario "create a constraint on accepted type field", {
    given "an integer field", {
        field = new NumberField(Integer)
    }
    and "a between constraint factory for integers", {
        factory = new BetweenConstraintFactory(1, 10)
    }
    when "a constraint is created using it", {
        constraint = factory.createConstraintFor(field)
    }
    then "the created constraint is not null", {
        constraint.shouldNotBe null
    }
    and "is an instance of BetweenConstraint", {
        constraint.shouldBeA BetweenConstraint
    }
    and "it should have desired min and max", {
        constraint.min.shouldBe 1
        constraint.max.shouldBe 10
    }
}

scenario "create a constraint on an invalid type field", {
    given "an object field", {
        field = new DummyField()
    }
    and "a between constraint factory for integers", {
        factory = new BetweenConstraintFactory(1, 10)
    }
    when "a constraint is created using it", {
        createConstraint = { -> factory.createConstraintFor(field) }
    }
    then "the creation should fail with an illegal argument exception", {
        ensureThrows(IllegalArgumentException) {
            createConstraint()
        }
    }
}

scenario "create a constraint on diferent type min and max", {
    given "min of different type from max", {
        min = (Integer) 1
        max = (BigInteger) 10
    }
    when "a between constraint factory is created with min of different type from max", {
        createFactory = { -> new BetweenConstraintFactory(min, max) }
    }
    then "the creation should fail with an illegal argument exception", {
        ensureThrows(IllegalArgumentException) {
            createConstraint()
        }
    }
}

scenario "create a constraint factory with null as min", {
    given "min as null", {
        min = null
        max = 10
    }
    when "a between constraint factory is created with min it", {
        createFactory = { -> new BetweenConstraintFactory(min, max) }
    }
    then "the creation should fail with an illegal argument exception", {
        ensureThrows(IllegalArgumentException) {
            createConstraint()
        }
    }
}

scenario "create a constraint factory with null as max", {
    given "max as null", {
        min = 0
        max = null
    }
    when "a between constraint factory is created with min it", {
        createFactory = { -> new BetweenConstraintFactory(min, max) }
    }
    then "the creation should fail with an illegal argument exception", {
        ensureThrows(IllegalArgumentException) {
            createConstraint()
        }
    }
}